
const app = getApp();
import { confirmSignIn, ConfirmationCode } from "../../../api/my"
import { getStorage } from "../../../utils/localStorage"
import { baseUrl } from "../../../api/release"
import { base64decode } from "../../../utils/util"
Page({
    data: {
        navData: {
            level: 2,
            title: "扫码签到"
        },
        user: getStorage('user'),
        imgUrl: baseUrl,
        navMargin: app.globalData.navHeight + app.globalData.navTop,
        initData: {},
        state: null, // 1：签到成功 2：签到失败 3.暂无权限
        code: null,
        scene: {
            id: null,
            activityId: null,
            userId: null
        },
    },
    onLoad(query) {
        let scene = decodeURIComponent(query.scene);
        this.setData({
            "scene.activityId": scene.substring(scene.indexOf("=") + 1, scene.indexOf("&")),
            "scene.userId": scene.substring(scene.lastIndexOf("=") + 1, scene.length),
            "scene.id": this.data.user.id
        });
        this.getconfirmSignIn();
    },
    onReady() {

    },
    onShow() {

    },
    //获取页面初始化数据
    getconfirmSignIn() {
        confirmSignIn(this.data.scene).then(res => {
            if (res.data.code === 200) {
                this.setData({
                    initData: res.data.data,
                    state: 1
                });
            } else {
                this.setData({
                    state: 2
                });
            };
            if (res.data.msg == 0) {
                this.setData({
                    state: 3
                });
            };
            wx.showToast({
                icon: "none",
                title: res.data.msg,
            });

        });
    },
    //签到input框 bindinput事件
    activedM(e) {
        let val = e.detail.value;
        this.setData({
            code: val
        });
    },
    //签到按钮点击事件
    submit() {
        if (!this.data.code) {
            wx.showToast({
                title: '请先输入活动码',
                icon: 'none'
            });
        } else {
            ConfirmationCode({ activityCode: this.data.code }).then(res => {
                this.getconfirmSignIn();
            });
        }
    },
    //签到扫码点击事件
    scanningCode() {
        wx.scanCode({
            onlyFromCamera: true, //是否只能从相机扫码
            success: res => {
                let scene = base64decode(res.rawData);
                this.setData({
                    "scene.activityId": scene.substring(scene.indexOf("=") + 1, scene.indexOf("&")),
                    "scene.userId": scene.substring(scene.lastIndexOf("=") + 1, scene.length),
                });
                this.getconfirmSignIn();
            }
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})